<template>
	<view class="wrap bg-white">
		<Navbar title="填写原因" />
		<u-form :model="form" ref="uForm" :label-width="100">
			<u-form-item label="原因" required prop="$BPMN_COMMENT_">
				<u--textarea v-model="form.$BPMN_COMMENT_"  />
			</u-form-item>
			<u-button type="primary" :loading="loading" :disabled="loading" @click="submit">提交</u-button>
		</u-form>
	</view>
</template>

<script>
	import Upload from '@/components/upload.vue'
	import DictSelect from '@/components/dict-select.vue'
	import { putAction,getAction,postAction } from '@/api/manage.js'
	import { CURY_TYPE } from '@/utils/constant.js'
	import Navbar from '@/components/navbar.vue'
	export default {
		components:{ Upload,DictSelect,Navbar },
		data() {
			return {
				form: {
					$BPMN_COMMENT_:'',
				},
				rules: {
					'$BPMN_COMMENT_': [
						{ 
							required: true, 
							message: '请填写原因', 
							trigger: ['blur'],
						}
					]
				},
				urlObj:{
					rejectlast: '/camunda/task/reject/last/', // 退回上一节点
					rejectfirst: '/camunda/task/reject/first/', // 退回发起人
					claim: '/camunda/task/claim/', // 拾取
				},
				loading:false,
				type:'',
				bpmnType:''
			}
		},
		methods: {
			submit(){
				this.loading = true
				this.$refs.uForm.validate().then(valid => {
					this.save()
				}).catch(errors => {
					setTimeout(() => {
						this.loading = false
					},1000)
				})
			},
			async save(){
				try{
					let url = ''
					if(this.type === 'rejectFirst'){
						url = this.urlObj.rejectfirst
					}else if(this.type === 'rejectLast'){
						url = this.urlObj.rejectlast
					}
					if(this.bpmnType === 'claim'){
						await postAction(this.urlObj.claim+this.id)
					}
					
					const res = await postAction(url+this.id,{
						...this.form,
					})
					const data = res.data
					if(data.success){
						this.$modal.msgSuccess('操作成功')
						// 回调流程发起表单方法
						if(this.type === 'rejectFirst'){
							this.updateBpmnStatus('INTERNALLY_TERMINATED')
						}
						setTimeout(() => {
							this.$Router.back(2)
						},1000)
					}else{
						this.$modal.msgError(data.message)
					}
					
				}catch(e){
					//TODO handle the exception
					setTimeout(() => {
						this.loading = false
					},1000)
				}
			},
			updateBpmnStatus(state) { // 自定义业务表单 ，签核完成后，更新业务单据流程状态
				this.curWebview = plus.webview.getWebviewById(this.viewId)
				const data = {state:state}
				this.curWebview.evalJS(`updateBpmnStatus(${JSON.stringify(data)})`)
			},
		},
		onReady() {
			this.$refs.uForm.setRules(this.rules)
		},
		onLoad(options) {
			const { id, type,viewId,bpmnType } =this.$Route.query
			this.id = id
			this.type = type
			this.viewId = viewId
			this.bpmnType = bpmnType
			console.log(plus.webview.getWebviewById(viewId).evalJS)
		}
	}
</script>
<style lang="scss" scoped>
	page {
		background-color: #ffffff;
	}
	.wrap{
		padding-left: 12px;
		padding-right: 12px;
	}
</style>
